﻿using System;
using NguyenHoa.Web.App_Code;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Net;
using System.IO;
using System.Data;
using NguyenHoa.Web.DataSets;

namespace NguyenHoa.Web.Controls
{
  public partial class PageView : System.Web.UI.UserControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsPostBack && !Page.IsCallback)
      {
        LoadPageView();
        LoadExchange();
        DateTime date = DateTime.Now;
        if (date.DayOfWeek == DayOfWeek.Saturday)
        {
          date.AddDays(-1);
        }
        if (date.DayOfWeek == DayOfWeek.Sunday)
        {
          date.AddDays(-2);
        }
        ltrUpdateDate.Text = "Ngày cập nhật : " + DateTime.Now.ToString("dd/MM/yyyy");
      }
    }

    private void LoadPageView()
    {
      string pageView = GlobalCache.Instance.PageView.ToString();
      int count = GlobalCache.Instance.PageViewDigit;
      if (pageView.Length > count)
        pageView = pageView.Remove(0, pageView.Length - count);
      else if (pageView.Length < count)
      {
        count = count - pageView.Length;
        for (int i = 0; i < count; i++)
          pageView = pageView.Insert(0, "0");
      }

      for (int i = 0; i < pageView.Length; i++)
      {
        string imgSrc = "../images/num_" + pageView[i] + ".gif";
        ltrPageView.Text += "<img src=\"" + imgSrc + "\" alt=\"\" width=\"20\" height=\"26\"/>";
      }
    }

    private void LoadExchange()
    {
      try
      {
        Match match;
       
        string strHTML = this.GetWebContent(ConfigurationSettings.AppSettings["VietcombankLink"].ToString());
        strHTML = Regex.Replace(strHTML, ConfigurationSettings.AppSettings["Regex1"].ToString(), "");
        match = Regex.Match(strHTML, ConfigurationSettings.AppSettings["RegexVietcombank"].ToString());
        dsExchange.tblExchangeDataTable tblExchangeTable = new dsExchange.tblExchangeDataTable();
        while (match.Success)
        {
          switch (match.Groups["ExchangeName"].ToString())
          {
            case "USD":
            case "JPY":
            case "GBP":
            case "EUR":
            case "AUD":
              string buy = match.Groups["ExchangeRate2"].ToString();
              string sell = match.Groups["ExchangeRate3"].ToString();
              DataRow row = tblExchangeTable.NewRow();
              row["Name"] = match.Groups["ExchangeName"].ToString();
              row["Buy"] = buy;
              row["Sell"] = sell;
              tblExchangeTable.Rows.Add(row);
              break;
          }
          match = match.NextMatch();
        }

        gvAd.DataSource = tblExchangeTable;
        gvAd.DataBind();
      }
      catch (Exception ex)
      {
      }
    }

    public string GetWebContent(string strLink)
    {
      string strContent = "";
      try
      {
        WebRequest objWebRequest = WebRequest.Create(strLink);

        objWebRequest.Credentials = CredentialCache.DefaultCredentials;

        WebResponse objWebResponse = objWebRequest.GetResponse();
        Stream receiveStream = objWebResponse.GetResponseStream();
        StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.UTF8);

        strContent = readStream.ReadToEnd();
        objWebResponse.Close();
        readStream.Close();
      }
      catch (Exception ex)
      {
      }
      return strContent;
    }
  }
}